﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace SalePriceSystem.Page
{
    public partial class login : System.Web.UI.Page
    {
        string domain = WebConfigurationManager.AppSettings["ADDomainName"] ?? "KCUS";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //string ADAuthen = HttpContext.Current.User.Identity.Name;
                //if (!string.IsNullOrEmpty(ADAuthen))
                //{

                //    domain = domain + "\\";
                //    ADAuthen = ADAuthen.Replace(domain.ToLower(), "");
                //    ADAuthen = ADAuthen.Replace(domain.ToUpper(), "");
                //    List<tblUser> uAuthen = Controller.Helper.Member.GetUserDB(ADAuthen);
                //    if (uAuthen.Count > 0)
                //    {
                //        Controller.AD.LdapAuthentication.SetAuthen(uAuthen[0]);
                //        redirectRole(uAuthen[0].RoleName);
                //    }
                //}

                string ADAuthen = HttpContext.Current.User.Identity.Name;
                ADAuthen = ADAuthen.Replace(domain.ToLower() + "\\", "");
                ADAuthen = ADAuthen.Replace(domain.ToUpper() + "\\", "");
                
                TextBoxUser.Text = ADAuthen;
                var Represendto = new HttpCookie("Represendto");
                Represendto.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(Represendto);
                var salegrp = new HttpCookie("salegrp");
                salegrp.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(salegrp);
                var username = new HttpCookie("username");
                username.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(username);
                var userrole = new HttpCookie("userrole");
                userrole.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(userrole);
                if (!string.IsNullOrEmpty(ADAuthen))
                {
                    List<tblUser> uAuthen = Controller.Helper.Member.GetUserDB(ADAuthen);
                    if (uAuthen.Count > 0)
                    {
                        Controller.AD.LdapAuthentication.SetAuthen(uAuthen[0]);
                        redirectRole(uAuthen[0].RoleName);
                    }
                }

            }

            //Response.Write("User");
            //Response.Write(HttpContext.Current.User.Identity.IsAuthenticated);
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            string user = TextBoxUser.Text;
            string pass = TextBoxPassword.Text;

            if (!string.IsNullOrEmpty(user) && user == "admin")
            {
                if (!string.IsNullOrEmpty(pass) && pass == "admin")
                {
                    var testuser = new tblUser();
                    testuser.UserName = "admin";
                    testuser.RoleName = "Admin";
                    testuser.msalegrp = "";
                    Controller.AD.LdapAuthentication.SetAuthen(testuser);
                    Response.Redirect("/page/AdminDocumentPage.htm");
                }
            }
            //if (!string.IsNullOrEmpty(user))
            //{
            //    List<tblUser> uAuthen = Controller.Helper.Member.GetUserDB(user);
            //    if (uAuthen.Count > 0)
            //    {
            //        if (uAuthen[0].Password.Equals(pass))
            //        {
            //            Controller.AD.LdapAuthentication.SetAuthen(uAuthen[0]);
            //            redirectRole(uAuthen[0].RoleName);
            //        }
                    
            //    }
                    
                
            //}

            //string ADAuthen = HttpContext.Current.User.Identity.Name;
            //if (!string.IsNullOrEmpty(ADAuthen))
            //{
                
            //    domain = domain + "\\";
            //    ADAuthen = ADAuthen.Replace(domain.ToLower(), "");
            //    ADAuthen = ADAuthen.Replace(domain.ToUpper(), "");
            //    List<tblUser> uAuthen = Controller.Helper.Member.GetUserDB(ADAuthen);
            //    if (uAuthen.Count > 0)
            //    {
            //        Controller.AD.LdapAuthentication.SetAuthen(uAuthen[0]);
            //        redirectRole(uAuthen[0].RoleName);
            //    }
            //}
            
            List<tblUser> us = Controller.Helper.Member.GetUserDB(user);
            if (us.Count > 0)
            {
                tblUser u = us[0];
                if (u.IsDisable == true)
                {
                    LiteralMessage.Visible = true;
                    LiteralMessage.Text = "The user is disable, Please contact your admin.";
                    return;
                }
                if (pass == u.Password)
                {
                    Controller.AD.LdapAuthentication.SetAuthen(u);
                    redirectRole(u.RoleName);
                }
            }
            LiteralMessage.Visible = true;
            LiteralMessage.Text = "User or Password incorrect.";
        }
        private void redirectRole(string roleName)
        {
            switch (roleName.ToLower())
            {
                case "sale":
                    Response.Redirect("/page/SaleCustomerManagement.htm");
                    break;
                case "approver":
                    Response.Redirect("/page/ApprovalPage.htm");
                    break;
                case "admin":
                    Response.Redirect("/page/AdminDocumentPage.htm");
                    break;
                case "saleco":
                    Response.Redirect("/page/saleCoMain.htm");
                    break;
            }
        }
    }
}